#ifndef _VL_FUTURE__H_
#define _VL_FUTURE__H_

#include <memory>
#include <condition_variable>
#include <mutex>
#include <vector>
#include <functional>

namespace VL {

/**
 * @Author: CimZzz
 * @Date: 2022-09-16 10:38:04
 * 
 * 所有 Future 的基类 
 */
class VLFutureBase: public std::enable_shared_from_this<VLFutureBase> {
public:

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 15:17:36
	 * 
	 * 中断当前 Future 任务
	 */
	virtual void interrupt();

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 08:17:08
	 * 
	 * 等待结果
	 */
	void wait();

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 08:17:50
	 * 
	 * 判断 Future 是否已经完成
	 * 
	 * @return 如果成功返回 true
	 */
	bool isCompleted();

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 08:17:50
	 * 
	 * 判断 Future 是否已经中断
	 * 
	 * @return 如果中断返回 true
	 */
	bool isInterrupted();

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 16:28:12
	 * 
	 * 获取数据, 否则抛出异常
	 */
	virtual void getOrThrow();

protected:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 10:39:10
	 * 
	 * 构造方法
	 */
	explicit VLFutureBase();

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 08:18:15
	 * 
	 * 判断 Future 是否已经完成
	 */
	bool completed;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 15:15:47
	 * 
	 * 判断 Future 是否中断
	 */
	bool interrupted;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 08:15:54
	 * 
	 * 线程锁
	 */
	std::mutex mutex;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 08:16:08
	 * 
	 * 条件变量
	 */
	std::condition_variable cond;
public:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 10:39:15
	 * 
	 * 析构方法
	 */
	virtual ~VLFutureBase();
};

template<class T>
class VLFutureBaseInherit: virtual public VLFutureBase {
public:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 10:41:52
	 * 
	 * 获取当前对象的智能指针
	 * 
	 * @return 当前对象的指针指针
	 */
    std::shared_ptr<T> shared_from_this() { 
    	return std::dynamic_pointer_cast<T>(VLFutureBase::shared_from_this()); 
    } 

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 10:42:39
	 * 
	 * 向下转型为对应类型的智能指针
	 * 
	 * @return 向下转型的智能指针
	 */
    template <class Down> 
    std::shared_ptr<Down> downcasted_shared_from_this() { 
    	return std::dynamic_pointer_cast<Down>(VLFutureBase::shared_from_this()); 
    } 
};

/**
 * @Author: CimZzz
 * @Date: 2022-09-20 19:00:16
 * 
 * 预声明 VLFuture
 */
class VLFuture;

/**
 * @Author: CimZzz
 * @Date: 2022-09-20 14:59:20
 * 
 * Future 核心
 */
template<typename Data, typename Exception>
class VLFutureCore: virtual public VLFutureBaseInherit<VLFutureCore<Data, Exception>> {
protected:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 18:09:10
	 * 
	 * 构造方法
	 */
	explicit VLFutureCore(): thenListPtr(nullptr), errorListPtr(nullptr),
		completedListPtr(nullptr), isHasError(false), dataPtr(nullptr), exceptionPtr(nullptr) {

	}

public:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:04:03
	 * 
	 * Future 结果回调
	 * 
	 * 当 Future 获得数据后, 会触发该回调
	 */
	using ThenCallback = std::function<void(const std::shared_ptr<Data>& dataPtr)>;
	
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:04:03
	 * 
	 * Future 异常回调
	 * 
	 * 当 Future 发生异常后, 会触发该回调
	 */
	using ErrorCallback = std::function<void(const std::shared_ptr<Exception>& errorPtr)>;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:04:03
	 * 
	 * Future 完成回调
	 * 
	 * 无论 Future 获得数据还是发生异常, 都会在之后调用该回调
	 */
	using CompletedCallback = std::function<void()>;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:22:21
	 * 
	 * Future 链式回调
	 * 
	 * 只有在 Future 获得数据后才会调用该方法
	 */
	template<typename FromData, typename FromException, typename ToData, typename ToException>
	using ChainCallback = std::function<std::shared_ptr<VLFutureCore<ToData, ToException>>(const std::shared_ptr<FromData>&)>;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:27:07
	 * 
	 * 添加 Future 结果回调
	 * 
	 * @param thenCallback 结果回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> then(ThenCallback thenCallback) {
		// 子类实现
		return this->shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:47:25
	 * 
	 * 添加 Future 异常回调
	 * 
	 * @param errorCallback 异常回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> error(ErrorCallback errorCallback) {
		// 子类实现
		return this->shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:49:09
	 * 
	 * 添加 Future 完成回调
	 * 
	 * @param completedCallback 完成回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> complete(CompletedCallback completedCallback) {
		// 子类实现
		return this->shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 17:37:52
	 * 
	 * 链式转换 Future
	 * 
	 * @param chainCallback Future 链式回调
	 * 
	 * @return 转换后的 Future 指针
	 */
	template<typename ToData, typename ToException>
	std::shared_ptr<VLFutureCore<ToData, ToException>> chain(ChainCallback<Data, Exception, ToData, ToException> chainCallback);

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:52:19
	 * 
	 * 设置 Future 数据
	 * 
	 * @param data 数据
	 */
	virtual void setData(const Data& data) {
		this->setData(std::shared_ptr<Data>(new Data(data)));
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:52:56
	 * 
	 * 设置 Future 数据
	 * 
	 * @param dataPtr 数据指针
	 */
	virtual void setData(Data* dataPtr) {
		this->setData(std::shared_ptr<Data>(dataPtr));
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:53:42
	 * 
	 * 设置 Future 数据
	 * 
	 * @param dataPtr 数据指针
	 */
	virtual void setData(const std::shared_ptr<Data>& dataPtr) {
		// 子类实现
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:55:05
	 * 
	 * 设置 Future 异常
	 * 
	 * @param exception 异常数据
	 */
	virtual void setError(const Exception& exception) {
		this->setError(std::shared_ptr<Exception>(new Exception(exception)));
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:55:34
	 * 
	 * 设置 Future 异常
	 * 
	 * @param exceptionPtr 异常指针
	 */
	virtual void setError(Exception* exceptionPtr) {
		this->setError(std::shared_ptr<Exception>(exceptionPtr));
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:56:05
	 * 
	 * 设置 Future 异常
	 * 
	 * @param exceptionPtr 异常指针
	 */
	virtual void setError(const std::shared_ptr<Exception>& exceptionPtr) {
		// 子类实现
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 14:54:23
	 * 
	 * 获取数据, 如果发生异常则会抛出异常
	 * 
	 * @return 数据指针
	 */
	virtual std::shared_ptr<Data> getDataOrThrow() {
		// 子类实现
		return std::shared_ptr<Data>(nullptr);
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 13:12:06
	 * 
	 * 获取数据, 如果发生异常则会返回 nullptr
	 * 
	 * @return 数据指针
	 */
	virtual std::shared_ptr<Data> getData() {
		// 子类实现
		return std::shared_ptr<Data>(nullptr);
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 13:13:07
	 * 
	 * 获取异常
	 * 
	 * @return 异常指针
	 */
	virtual std::shared_ptr<Exception> getException() {
		// 子类实现
		return std::shared_ptr<Exception>(nullptr);
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 18:43:37
	 * 
	 * 父级 Future 指针
	 */
	std::shared_ptr<VLFutureBase> parentFuturePtr;

protected:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 19:22:09
	 * 
	 * 被指定 Future 中抢夺走全部回调
	 * 
	 * @param grabFuturePtr 抢夺的 Future 指针
	 */
	void grabCallback(std::shared_ptr<VLFutureCore<Data, Exception>>& grabFuturePtr) {
		grabFuturePtr->thenListPtr = this->thenListPtr;
		grabFuturePtr->errorListPtr = this->errorListPtr;
		grabFuturePtr->completedListPtr = this->completedListPtr;
		grabFuturePtr->completed = this->completed;
		grabFuturePtr->interrupted = this->interrupted;
		grabFuturePtr->isHasError = this->isHasError;
		grabFuturePtr->dataPtr = this->dataPtr;
		grabFuturePtr->exceptionPtr = this->exceptionPtr;

		this->thenListPtr.reset();
		this->errorListPtr.reset();
		this->completedListPtr.reset();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 17:47:26
	 * 
	 * 添加结果回调到内部回调列表里
	 * 
	 * @param callback Future 结果回调
	 */
	void addThenCallback(ThenCallback callback) {
		if(this->thenListPtr.get() == nullptr) {
			this->thenListPtr.reset(new std::vector<VLFutureCore<Data, Exception>::ThenCallback>);
		}
		this->thenListPtr->push_back(callback);
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 17:49:18
	 * 
	 * 添加异常回调到内部回调列表里
	 * 
	 * @param callback Future 异常回调
	 */
	void addErrorCallback(ErrorCallback callback) {
		if(this->errorListPtr.get() == nullptr) {
			this->errorListPtr.reset(new std::vector<VLFutureCore<Data, Exception>::ErrorCallback>);
		}
		this->errorListPtr->push_back(callback);
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 17:50:42
	 * 
	 * 添加完成回调到内部回调列表里
	 * 
	 * @param callback Future 完成回调
	 */
	void addCompletedCallback(CompletedCallback callback) {
		if(this->completedListPtr.get() == nullptr) {
			this->completedListPtr.reset(new std::vector<VLFutureCore<Data, Exception>::CompletedCallback>);
		}
		this->completedListPtr->push_back(callback);
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-27 17:48:04
	 * 
	 * 通知触发 Future 结果回调
	 */
	void notifyThenList() {
		if(this->thenListPtr.get() != nullptr) {
			for(auto iter = this->thenListPtr->begin() ; iter != this->thenListPtr->end() ; iter ++) {
				(*iter)(this->dataPtr);
			}
		}
		this->notifyCompletedList();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-27 17:48:21
	 * 
	 * 通知触发 Future 异常回调
	 */
	void notifyErrorList() {
		if(this->errorListPtr.get() != nullptr) {
			for(auto iter = this->errorListPtr->begin() ; iter != this->errorListPtr->end() ; iter ++) {
				(*iter)(this->exceptionPtr);
			}
		}
		this->notifyCompletedList();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-27 17:48:29
	 * 
	 * 通知触发 Future 完成回调
	 */
	void notifyCompletedList() {
		if(this->completedListPtr.get() != nullptr) {
			for(auto iter = this->completedListPtr->begin() ; iter != this->completedListPtr->end() ; iter ++) {
				(*iter)();
			}
		}
		this->thenListPtr.reset();
		this->errorListPtr.reset();
		this->completedListPtr.reset();
	}


	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-21 11:16:52
	 * 
	 * 检查 Future 中是否存在满足触发条件的 Callback
	 * 
	 * @param grabFuturePtr 通过 Chain 生成的 Future 指针
	 * 
	 * 应用在链式 Future 中, 当一个 Future 在返回时就已经拥有了结果, 需要调用该方法主动触发一下激活回调
	 */
	void checkCallback(std::shared_ptr<VLFutureCore<Data, Exception>>& grabFuturePtr) {
		grabFuturePtr->mutex.lock();
		if(grabFuturePtr->interrupted) {
			grabFuturePtr->mutex.unlock();
			grabFuturePtr->notifyCompletedList();
			return;
		}
		if(grabFuturePtr->completed) {
			this->mutex.unlock();
			if(grabFuturePtr->isHasError) {
				grabFuturePtr->notifyErrorList();
			} else {
				grabFuturePtr->notifyThenList();
			}
			return;
		}
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-27 17:37:14
	 * 
	 * Future 结果回调列表指针
	 */
	typename std::shared_ptr<std::vector<VLFutureCore<Data, Exception>::ThenCallback>> thenListPtr;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-27 17:37:14
	 * 
	 * Future 失败回调列表指针
	 */
	typename std::shared_ptr<std::vector<VLFutureCore<Data, Exception>::ErrorCallback>> errorListPtr;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-27 17:37:14
	 * 
	 * Future 完成回调列表指针
	 */
	typename std::shared_ptr<std::vector<VLFutureCore<Data, Exception>::CompletedCallback>> completedListPtr;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 18:27:35
	 * 
	 * 是否发生异常
	 */
	bool isHasError;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 18:27:08
	 * 
	 * 数据指针
	 */
	std::shared_ptr<Data> dataPtr;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 18:27:43
	 * 
	 * 异常指针
	 */
	std::shared_ptr<Exception> exceptionPtr;
};

/**
 * @Author: CimZzz
 * @Date: 2022-08-21 08:15:14
 * 
 * Future 基类
 */
class VLFuture: public VLFutureBaseInherit<VLFuture> {
public:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 19:01:13
	 * 
	 * 实例化 Future 对象
	 * 
	 * @return 带有类型的 Future 对象指针
	 */
	template<typename T, typename E>
	static std::shared_ptr<VLFutureCore<T, E>> newTypeFuture();

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 19:01:13
	 * 
	 * 实例化 Future 对象
	 * 
	 * @return 消除类型的 Future 对象指针
	 */
	template<typename T, typename E>
	static std::shared_ptr<VLFuture> newFuture();

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-27 17:15:25
	 * 
	 * 转化为 Future Core 类型指针
	 */
	template<typename T, typename E>
	std::shared_ptr<VLFutureCore<T, E>> as() {
		return this->downcasted_shared_from_this<VLFutureCore<T, E>>();
	}
protected:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 08:16:38
	 * 
	 * 保护构造函数
	 */
	explicit VLFuture();
};

template<typename Data, typename Exception, typename FromData, typename FromException>
class VLChainFuture: virtual public VLFuture, virtual public VLFutureCore<Data, Exception> {
public:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:27:07
	 * 
	 * 添加 Future 结果回调
	 * 
	 * @param thenCallback 结果回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> then(typename VLFutureCore<Data, Exception>::ThenCallback thenCallback) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			if(!this->interrupted && !this->isHasError) {
				thenCallback(this->dataPtr);
			}
			return VLFutureCore<Data, Exception>::shared_from_this();
		}
		this->addThenCallback(thenCallback);
		this->mutex.unlock();
		return VLFutureCore<Data, Exception>::shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:47:25
	 * 
	 * 添加 Future 异常回调
	 * 
	 * @param errorCallback 异常回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> error(typename VLFutureCore<Data, Exception>::ErrorCallback errorCallback) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			if(!this->interrupted && this->isHasError) {
				errorCallback(this->exceptionPtr);
			}
			return VLFutureCore<Data, Exception>::shared_from_this();
		}
		this->addErrorCallback(errorCallback);
		this->mutex.unlock();
		return VLFutureCore<Data, Exception>::shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:49:09
	 * 
	 * 添加 Future 完成回调
	 * 
	 * @param completedCallback 完成回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> complete(typename VLFutureCore<Data, Exception>::CompletedCallback completedCallback) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			completedCallback();
			return VLFutureCore<Data, Exception>::shared_from_this();
		}
		this->addCompletedCallback(completedCallback);
		this->mutex.unlock();
		return VLFutureCore<Data, Exception>::shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:53:42
	 * 
	 * 设置 Future 数据
	 * 
	 * @param dataPtr 数据指针
	 */
	virtual void setData(const std::shared_ptr<Data>& dataPtr) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			return;
		}
		this->completed = true;
		this->isHasError = false;
		this->dataPtr = dataPtr;
		this->mutex.unlock();
		this->cond.notify_all();
		this->notifyThenList();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:56:05
	 * 
	 * 设置 Future 异常
	 * 
	 * @param exceptionPtr 异常指针
	 */
	virtual void setError(const std::shared_ptr<Exception>& exceptionPtr) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			return;
		}
		this->completed = true;
		this->isHasError = true;
		this->exceptionPtr = exceptionPtr;
		this->mutex.unlock();
		this->cond.notify_all();
		this->notifyErrorList();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 14:54:23
	 * 
	 * 获取数据, 如果发生异常则会抛出异常
	 * 
	 * @return 数据指针
	 */
	virtual std::shared_ptr<Data> getDataOrThrow() {
		std::unique_lock<std::mutex> locker(this->mutex);
		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}
		if(!this->completed) {
			this->cond.wait(locker);
		}

		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}

		if(this->isHasError) {
			throw *this->exceptionPtr;
		}

		return this->dataPtr;
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 13:12:06
	 * 
	 * 获取数据, 如果发生异常则会返回 nullptr
	 * 
	 * @return 数据指针
	 */
	virtual std::shared_ptr<Data> getData() {
		std::unique_lock<std::mutex> locker(this->mutex);
		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}
		if(!this->completed) {
			this->cond.wait(locker);
		}

		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}

		return this->dataPtr;
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 13:13:07
	 * 
	 * 获取异常
	 * 
	 * @return 异常指针
	 */
	virtual std::shared_ptr<Exception> getException() {
		std::unique_lock<std::mutex> locker(this->mutex);
		if(this->interrupted) {
			return std::shared_ptr<Exception>(nullptr);
		}
		if(!this->completed) {
			this->cond.wait(locker);
		}

		if(this->interrupted) {
			return std::shared_ptr<Exception>(nullptr);
		}

		return this->exceptionPtr;
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 15:17:36
	 * 
	 * 中断当前 Future 任务
	 */
	virtual void interrupt() {
		// 子类实现
		this->mutex.lock();
		if(this->chainFuturePtr.get() != nullptr) {
			this->mutex.unlock();
			this->chainFuturePtr->interrupt();
			return;
		}
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			return;
		}
		this->interrupted = true;
		this->mutex.unlock();
		this->cond.notify_all();
		this->notifyCompletedList();
	}

private:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 19:11:28
	 * 
	 * 声明 VLFutureCore 为友元类
	 */
	friend class VLFutureCore<FromData, FromException>;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:22:21
	 * 
	 * Future 链式回调
	 * 
	 * 只有在 Future 获得数据后才会调用该方法
	 */
	using ChainCallback = std::function<std::shared_ptr<VLFutureCore<Data, Exception>>(const std::shared_ptr<FromData>&)>;


	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 18:16:42
	 * 
	 * 构造函数
	 * 
	 */
	explicit VLChainFuture(ChainCallback chainCallback): chainCallback(chainCallback) {
	}


	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-16 19:00:02
	 * 
	 * 创建链式 Future
	 * 
	 * @param dataPtr 来源数据
	 */
	void makeChain(const std::shared_ptr<FromData>& dataPtr) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			return;
		}
		this->chainFuturePtr = this->chainCallback(dataPtr);
		this->chainFuturePtr->parentFuturePtr = VLFutureBase::shared_from_this();
		this->mutex.unlock();
		this->chainFuturePtr->then([&](const std::shared_ptr<Data>& selfDataPtr) {
			this->setData(selfDataPtr);
		})->error([&](const std::shared_ptr<Exception>& selfExceptionPtr) {
			this->setError(selfExceptionPtr);
		});
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 18:15:35
	 * 
	 * Future 链式回调
	 */
	ChainCallback chainCallback;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 18:38:39
	 * 
	 * 通过 ChainCallback 生成的 Future 对象指针
	 */
	std::shared_ptr<VLFutureCore<Data, Exception>> chainFuturePtr;
};

/**
 * @Author: CimZzz
 * @Date: 2022-09-20 16:14:39
 * 
 * 值 Future
 */
template<typename Data, typename Exception>
class VLValueFuture: virtual public VLFuture, virtual public VLFutureCore<Data, Exception> {
public:

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:27:07
	 * 
	 * 添加 Future 结果回调
	 * 
	 * @param thenCallback 结果回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> then(typename VLFutureCore<Data, Exception>::ThenCallback thenCallback) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			if(!this->interrupted && !this->isHasError) {
				thenCallback(this->dataPtr);
			}
			return VLFutureCore<Data, Exception>::shared_from_this();
		}
		this->addThenCallback(thenCallback);
		this->mutex.unlock();
		return VLFutureCore<Data, Exception>::shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:47:25
	 * 
	 * 添加 Future 异常回调
	 * 
	 * @param errorCallback 异常回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> error(typename VLFutureCore<Data, Exception>::ErrorCallback errorCallback) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			if(!this->interrupted && this->isHasError) {
				errorCallback(this->exceptionPtr);
			}
			return VLFutureCore<Data, Exception>::shared_from_this();
		}
		this->addErrorCallback(errorCallback);
		this->mutex.unlock();
		return VLFutureCore<Data, Exception>::shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:49:09
	 * 
	 * 添加 Future 完成回调
	 * 
	 * @param completedCallback 完成回调
	 * 
	 * @return FutureCore 指针
	 */
	virtual std::shared_ptr<VLFutureCore<Data, Exception>> complete(typename VLFutureCore<Data, Exception>::CompletedCallback completedCallback) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			completedCallback();
			return VLFutureCore<Data, Exception>::shared_from_this();
		}
		this->addCompletedCallback(completedCallback);
		this->mutex.unlock();
		return VLFutureCore<Data, Exception>::shared_from_this();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:53:42
	 * 
	 * 设置 Future 数据
	 * 
	 * @param dataPtr 数据指针
	 */
	virtual void setData(const std::shared_ptr<Data>& dataPtr) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			return;
		}
		this->completed = true;
		this->isHasError = false;
		this->dataPtr = dataPtr;
		this->mutex.unlock();
		this->cond.notify_all();
		this->notifyThenList();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 15:56:05
	 * 
	 * 设置 Future 异常
	 * 
	 * @param exceptionPtr 异常指针
	 */
	virtual void setError(const std::shared_ptr<Exception>& exceptionPtr) {
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			return;
		}
		this->completed = true;
		this->isHasError = true;
		this->exceptionPtr = exceptionPtr;
		this->mutex.unlock();
		this->cond.notify_all();
		this->notifyErrorList();
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 14:54:23
	 * 
	 * 获取数据, 如果发生异常则会抛出异常
	 * 
	 * @return 数据指针
	 */
	virtual std::shared_ptr<Data> getDataOrThrow() {
		std::unique_lock<std::mutex> locker(this->mutex);
		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}
		if(!this->completed) {
			this->cond.wait(locker);
		}

		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}

		if(this->isHasError) {
			throw *this->exceptionPtr;
		}

		return this->dataPtr;
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 13:12:06
	 * 
	 * 获取数据, 如果发生异常则会返回 nullptr
	 * 
	 * @return 数据指针
	 */
	virtual std::shared_ptr<Data> getData() {
		std::unique_lock<std::mutex> locker(this->mutex);
		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}
		if(!this->completed) {
			this->cond.wait(locker);
		}

		if(this->interrupted) {
			return std::shared_ptr<Data>(nullptr);
		}

		return this->dataPtr;
	}

	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 13:13:07
	 * 
	 * 获取异常
	 * 
	 * @return 异常指针
	 */
	virtual std::shared_ptr<Exception> getException() {
		std::unique_lock<std::mutex> locker(this->mutex);
		if(this->interrupted) {
			return std::shared_ptr<Exception>(nullptr);
		}
		if(!this->completed) {
			this->cond.wait(locker);
		}

		if(this->interrupted) {
			return std::shared_ptr<Exception>(nullptr);
		}

		return this->exceptionPtr;
	}


	/**
	 * @Author: CimZzz
	 * @Date: 2022-08-21 15:17:36
	 * 
	 * 中断当前 Future 任务
	 */
	virtual void interrupt() {
		// 子类实现
		this->mutex.lock();
		if(this->completed || this->interrupted) {
			this->mutex.unlock();
			return;
		}
		this->interrupted = true;
		this->mutex.unlock();
		this->cond.notify_all();
		this->notifyCompletedList();
	}

private:
	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 19:09:40
	 * 
	 * 声明友元类
	 */
	friend class VLFuture;

	/**
	 * @Author: CimZzz
	 * @Date: 2022-09-20 19:10:17
	 * 
	 * 私有构造函数
	 */
	explicit VLValueFuture() {}
};

/**
 * @Author: CimZzz
 * @Date: 2022-09-20 17:37:52
 * 
 * 链式转换 Future
 * 
 * @param chainCallback Future 链式回调
 * 
 * @return 转换后的 Future 指针
 */
template<typename Data, typename Exception>
template<typename ToData, typename ToException>
std::shared_ptr<VLFutureCore<ToData, ToException>> VLFutureCore<Data, Exception>::chain(ChainCallback<Data, Exception, ToData, ToException> chainCallback) {
	std::shared_ptr<VLChainFuture<ToData, ToException, Data, Exception>> proxyFuturePtr(new VLChainFuture<ToData, ToException, Data, Exception>(chainCallback));
	proxyFuturePtr->parentFuturePtr = std::dynamic_pointer_cast<VLFutureBase>(this->shared_from_this());
	this->then([proxyFuturePtr](const std::shared_ptr<Data>& dataPtr) {
		proxyFuturePtr->makeChain(dataPtr);
	});
	return std::dynamic_pointer_cast<VLFutureCore<ToData, ToException>>(proxyFuturePtr);
}

/**
 * @Author: CimZzz
 * @Date: 2022-09-20 19:01:13
 * 
 * 实例化 Future 对象
 * 
 * @return 带有类型的 Future 对象指针
 */
template<typename T, typename E>
std::shared_ptr<VLFutureCore<T, E>> VLFuture::newTypeFuture() {
	std::shared_ptr<VLFutureCore<T, E>> futurePtr(new VLValueFuture<T, E>());
	return futurePtr;
}

/**
 * @Author: CimZzz
 * @Date: 2022-09-20 19:01:13
 * 
 * 实例化 Future 对象
 * 
 * @return 消除类型的 Future 对象指针
 */
template<typename T, typename E>
std::shared_ptr<VLFuture> VLFuture::newFuture() {
	std::shared_ptr<VLFutureCore<T, E>> futurePtr(new VLValueFuture<T, E>());
	return std::dynamic_pointer_cast<VLFuture>(futurePtr);
}

}

#endif // _VL_FUTURE__H_
